package com.complex.spring.advance.aop.base;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;

/**
 * 基于接口的代理
 * @author gao
 *
 */
public class ProxyFactoryForInterface {
	public static void main(String[] args) {
		//代理目标对象
		MockTask task  = new MockTask();
		task.execute();
		//织入器 ProxyFactory
		ProxyFactory weaver = new ProxyFactory();
		/**设置目标对象*/
		weaver.setTarget(task);
		//设置需要实现的接口,如果只是实现MockTask实现的接口,这里可以不设置,默认实现
		weaver.setInterfaces(new Class[]{ITask.class});
		//设置advisor
		NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor();
		advisor.setMappedNames(new String[]{"execute"});
		advisor.setAdvice(new PerformanceMethodInterceptor());
		/**设置advisor*/
		weaver.addAdvisor(advisor);
		/**取得代理对象*/
		//注意只能上转型为ITask,不能转型成MockTask
		ITask proxyObject = (ITask)weaver.getProxy();
		//调用代理对象
		proxyObject.execute();
		System.out.println(proxyObject.getClass());
	}
}
